//
//  COProfileTableViewCell.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/29.
//

import UIKit

class COProfileTableViewCell: UITableViewCell {
    var model: COProfileModel? {
        didSet {
            guard let model = model else {return}
            
            nameLabel.text = model.name
            contentLabel.text = model.content
            contentImageView.isHidden = false
            if let image = model.image {
                contentImageView.image = image
            } else if let image = model.imageUrl {
                contentImageView.kf.setImage(with: URL(string: image))
            } else {
                contentImageView.isHidden = true
            }
            
            rightImageView.isHidden = model.rightHidden
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func configUI() {
        backgroundColor = .rgba(17, 7, 41, 1)
        contentView.addSubview(nameLabel)
        contentView.addSubview(stackView)
        stackView.addArrangedSubviews([contentLabel, contentImageView, rightImageView])
        contentView.addSubview(lineView)
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(22)
            make.centerY.equalToSuperview()
        }
        
        stackView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(10)
            make.right.equalToSuperview().offset(-20)
            make.centerY.equalToSuperview()
        }
        
        contentImageView.snp.makeConstraints { make in
            make.width.height.equalTo(36)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        nameLabel.setContentHuggingPriority(.required, for: .horizontal)
        nameLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        
        rightImageView.setContentHuggingPriority(.required, for: .horizontal)
        rightImageView.setContentCompressionResistancePriority(.required, for: .horizontal)

        contentImageView.setContentHuggingPriority(.required, for: .horizontal)
        contentImageView.setContentCompressionResistancePriority(.required, for: .horizontal)

    }
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .white
        return label
    }()

    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .horizontal
        stackV.alignment = .center
        stackV.spacing = 10
        return stackV
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .white
        label.textAlignment = .right
        return label
    }()
    
    private lazy var contentImageView: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 18
        return imgV
    }()
    
    private lazy var rightImageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "Rectangle 350"))
        return imgV
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .white.withAlphaComponent(0.15)
        return view
    }()
}
